//
//  LLVisitor.swift
//  IOSWeibo
//
//  Created by 廖灵全 on 16/5/12.
//  Copyright © 2016年 liao. All rights reserved.
//

import UIKit
// 定义协议
@objc protocol LLVisitorDelegate : NSObjectProtocol{
    // 2.定义协议方法
    /// 注册按钮被点击后的代理方法
     func LLVisitorRegister ()
    // 登录按钮的代理方法
     func LLVisitorLogin ()

}
//   自定义访客视图   未登录那个界面  
class LLVisitor: UIView {
   
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    // MARK: - 公开的方法
    /// 设置访客视图内容
    func setupVisitorInfo(message:String,imgae:String){
        iconView.image = UIImage(named:imgae)
        messageView.text = message
        //隐藏小房子
        homeView.hidden = true
        //隐藏遮盖
        coveView.hidden = true
     }
    
    /// 转轮旋转动画
    func rotationAnimation(){
      //创建核心动画
        let anim = CABasicAnimation(keyPath:"transform.rotation")
        anim.toValue = M_PI * 2
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        // 核心动画完成的时候不要移除
        anim.removedOnCompletion = false
        //将动画添加到层上面
        iconView.layer.addAnimation(anim, forKey: nil)

        
    }
    //设置UI
    private func prepareUI() {
    //将子控件添加进来
        //这代码把遮盖视图下面那白色改变成了遮盖视图的颜色
        self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)

    self.addSubview(iconView)
    self.addSubview(coveView)
    self.addSubview(homeView)
    self.addSubview(messageView)
    self.addSubview(rigbtn)
    self.addSubview(loginBtn)
        
        
     //取消系统对我们的约束
        coveView.translatesAutoresizingMaskIntoConstraints = false
     iconView.translatesAutoresizingMaskIntoConstraints = false
     homeView.translatesAutoresizingMaskIntoConstraints = false
     messageView.translatesAutoresizingMaskIntoConstraints = false
     rigbtn.translatesAutoresizingMaskIntoConstraints = false
     loginBtn.translatesAutoresizingMaskIntoConstraints = false

    //添加约束  最好只添加一次
           // 转轮
        /// CenterX和父控件的CenterX重合
        // item: 要添加约束的view
        // attribute: 要添加的View的属性
        // toItem: 要参照的view
        // attribute: 要参照View的属性
        // 将约束添加到view上面去
//    NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
       //将约束往父控件加
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        /// CenterY和父控件的CenterY重合
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0 ))
        //遮盖视图
        self.addConstraint(NSLayoutConstraint(item: coveView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coveView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coveView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coveView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: rigbtn , attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        //小房子
         self.addConstraint( NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
          self.addConstraint( NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        //信息位置
        self.addConstraint(NSLayoutConstraint(item: messageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        // CenterX 和父控件CenterX
        self.addConstraint(NSLayoutConstraint(item: messageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // Top和转轮底部
        self.addConstraint(NSLayoutConstraint(item: messageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //注册按钮
        self.addConstraint(NSLayoutConstraint(item: rigbtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: rigbtn, attribute: NSLayoutAttribute.LeadingMargin, relatedBy: NSLayoutRelation.Equal, toItem: messageView, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1, constant: -30))
        self.addConstraint(NSLayoutConstraint(item: rigbtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 40))
        self.addConstraint(NSLayoutConstraint(item: rigbtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 60))
       
        //登录按钮
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.RightMargin, relatedBy: NSLayoutRelation.Equal, toItem: messageView, attribute: NSLayoutAttribute.RightMargin, multiplier: 1, constant: 30))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: rigbtn, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: rigbtn, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        
    }
    
    
    //定义代理属性
    weak var delegate : LLVisitorDelegate?
    
      /// 所有的控件
    //转轮
    private lazy var iconView : UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_image_smallicon"))
    //小房子
    private lazy var  homeView : UIImageView = UIImageView(image : UIImage(named:"visitordiscover_feed_image_house"))
    //遮盖视图
    private lazy var coveView : UIImageView = (UIImageView(image:UIImage(named: "visitordiscover_feed_mask_smallicon")))
    //消息
    private lazy var messageView : UILabel = {
            let label = UILabel()
        
        //设置label
        label.text = "关注一些人,看看有什么傻逼事发生!"
        label.textColor = UIColor.orangeColor()
        label.font = UIFont.systemFontOfSize(16)
        label.textAlignment = NSTextAlignment.Center
        //有多行的时候会自动换行
        label.numberOfLines = 0
        //文本有多少个字   label就显示有多大
        label.sizeToFit()
        
        
        return label
    }()
    
   //注册按钮
    private lazy var rigbtn : UIButton = {
        let  btn = UIButton()
    
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.addTarget(self, action: #selector(LLVisitor.RegisterBtn), forControlEvents: UIControlEvents.TouchUpInside)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        return btn
    }()
    // MARK: - 按钮点击事件
    // 按钮点击事件方法是系统来调用, 当把按钮点击事件private后系统就找不到这个方法了
    // @objc: 让系统可以找到我们的方法
    //注册按钮的点击事件
    @objc private func RegisterBtn() {
        //点击按钮就触发事件  用代理或者闭包
        //view是不能modal出一个控制器的  需要事件传递给控制器
        delegate?.LLVisitorRegister()
        
    }
    //登录按钮的点击事件
    @objc private func login() {
        //
        delegate?.LLVisitorLogin()
        
    }
    lazy var loginBtn : UIButton = {
       
        let btn = UIButton()
//        btn.setImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.addTarget(self, action: #selector(LLVisitor.login), forControlEvents: UIControlEvents.TouchUpInside)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        return btn
        
    }()
    

}
